package cn.itcast.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.IMenuService;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class MenuAction extends BaseAction<Menu>{
	private static final long serialVersionUID = 1L;
	@Autowired 
	private IMenuService menuService;
	
	@Action(value="menu_list", results={@Result(name="success", type="json")})
	public String MenuList() {
		List<Menu> MenuList = menuService.findAll();
		ServletActionContext.getContext().getValueStack().push(MenuList);
		
		return SUCCESS;
	}
	
	@Action(value="menu_save", results={@Result(name="success", 
			type="redirect", location="pages/system/menu.html")})
	public String save() {
		menuService.save(model);
		return SUCCESS;
	}
	
	@Action(value="menu_showmenu", results={@Result(name="success", type="json")})
	public String showMenu() {
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		List<Menu> menus = menuService.findByUser(user);
		ServletActionContext.getContext().getValueStack().push(menus);
		
		return SUCCESS;
	}
}
